package com.example.myapplication9.week4.jsjf;

import com.example.myapplication9.week4.jsjf.exceptions.ElementNotFoundException;


/**
 * ArrayUnorderedList represents an array implementation of an unordered list.
 *
 * @author Lewis and Chase
 * @version 4.0
 */
public class ArrayUnorderedList<T> extends ArrayList<T> implements UnorderedListADT<T>
{

    public ArrayUnorderedList()
    {
        super();
    }


    public ArrayUnorderedList(int initialCapacity)
    {
        super(initialCapacity);
    }


    @Override
    public void addToFront(T element)
    {
      if(rear-1==list.length)
          expandCapacity();

    T []list1=(T[])(new Object[list.length]);
    for(int a=0;a<rear;a++)
    {
        list1[a]=list[a];
    }
      list[0]=element;
    for(int a=0;a<rear;a++)
    {
        list[a+1]=list1[a];
    }
    rear++;
    }


    @Override
    public void addToRear(T element)
    {
        if(rear-1==list.length)
            expandCapacity();

        list[rear]=element;
        rear++;
    }


    @Override
    public void addAfter(T element, T target)
    {
        if (size() == list.length)
            expandCapacity();

        int scan = 0;
		
		// 找到插入位置
        while (scan < rear && !target.equals(list[scan]))
            scan++;
      
        if (scan == rear)
            throw new ElementNotFoundException("UnorderedList");
    
        scan++;
		
		// shift elements up one
        for (int shift=rear; shift > scan; shift--)
            list[shift] = list[shift-1];

		// insert element
		list[scan] = element;
         rear++;
		modCount++;
    }
}
